#include "StdAfx.h"
#include "stdafx.h"
#include "ColorRGB.h"


unsigned char convertToColor(float _val);
unsigned char convertToColor(int _val);

namespace FEngine
{
	
	ColorRGB::ColorRGB()
		: red(255), green(255), blue(255)
	{
	}
	
	ColorRGB::ColorRGB(const ColorRGB& _col)
		: red(_col.red), green(_col.green), blue(_col.blue)
	{
	}

	ColorRGB::ColorRGB(unsigned char _r, unsigned char _g, unsigned char _b)
		: red(_r), green(_g), blue(_b)
	{
	}
	
	ColorRGB::ColorRGB(const Vector3& _col)
	{
		red		= convertToColor(_col.x);
		green	= convertToColor(_col.y);
		blue	= convertToColor(_col.z);
	}
	
	ColorRGB::ColorRGB(const Vector4& _col)
	{
		red		= convertToColor(_col.x);
		green	= convertToColor(_col.y);
		blue	= convertToColor(_col.z);
	}

	ColorRGB::~ColorRGB()
	{
	}
	
	ColorRGB& ColorRGB::operator=(const ColorRGB& _col)
	{
		if (this == &_col)
			return *this;
		
		red		= _col.red;
		green	= _col.green;
		blue	= _col.blue;

		return *this;
	}

	ColorRGB& ColorRGB::operator+=(const ColorRGB& _col)
	{
		red		= convertToColor(red + _col.red);
		green	= convertToColor(green + _col.green);
		blue	= convertToColor(blue + _col.blue);

		return *this;
	}
	
	ColorRGB& ColorRGB::operator-=(const ColorRGB& _col)
	{
		red		= convertToColor(red - _col.red);
		green	= convertToColor(green - _col.green);
		blue	= convertToColor(blue - _col.blue);

		return *this;
	}
	
	bool ColorRGB::operator==(const ColorRGB& _col) const
	{
		return (red == _col.red && green == _col.green && blue == _col.blue);
	}
	
	bool ColorRGB::operator!=(const ColorRGB& _col) const
	{
		return !(*this == _col);
	}
	
	Vector3 ColorRGB::toVector3() const
	{
		return Vector3(red, green, blue);
	}
	
	Vector4 ColorRGB::toVector4() const
	{
		return Vector4(red, green, blue, 255);
	}
	
	Vector4 ColorRGB::toShaderFormat() const
	{
		return Vector4(red, green, blue, 255) / 255.0f;
	}

} // namespace FEngine


unsigned char convertToColor(float _val)
{
	if (_val < 0)
		return 0;
	else if (_val > 255.0f)
		return 255;

	return (unsigned char)_val;
}

unsigned char convertToColor(int _val)
{
	if (_val < 0)
		return 0;
	else if (_val > 255)
		return 255;

	return (unsigned char)_val;
}
